
# This file creates Table SI.1 of the Supporting Information --------------

# install.packages("xtable")
# install.packages("ggplot2")
# install.packages("dplyr")

library(xtable)
library(ggplot2)
library(dplyr)

# read the output data for mother and fathers 
outtab_mom <- read.table("mother_post09.txt", 
                         header = FALSE, skip = 1)
                        
outtab_dad <- read.table("father_post09.txt", 
                         header = FALSE, skip = 1)

# read the F stats
f_mom <- read.table("f_stat_mothers_post09.txt")
f_dad <- read.table("f_stat_fathers_post09.txt")

# create empty table for mothers 
main_tab_mom <- 
  matrix(NA, nrow = 12, ncol = 6)

# assign column names
colnames(main_tab_mom) <-
  rep(c("2013 Local", "2014 European"), each = 3)

# assign row names
rownames(main_tab_mom) <- 
  c("Twinning in",
    "first parity",
    "3",
    "No. of children (2sls)",
    "5",
    "6",
    "Turnout if one child in first parity",
    "N",
    "Share with twins",
    "F test",
    "2009 turnout",
    "2007 covariates")

# assign values
for(i in 0:1){
  # assign reduced form estimates
  main_tab_mom[1, 1:3 + 3*i] <- round(outtab_mom[2:4 + 7*i, 2], 3)
  # assign reduced form standard errors
  main_tab_mom[2, 1:3 + 3*i] <- paste("(",round(outtab_mom[2:4 + 7*i, 3], 3), ")", sep = "")
  # assign reduced form confidence intervals
  main_tab_mom[3, 1:3 + 3*i] <- paste("[", 
                                      round(outtab_mom[2:4 + 7*i, 2] + qnorm(0.025) * round(outtab_mom[2:4 + 7*i, 3], 3), 3),
                                      "; ",
                                      round(outtab_mom[2:4 + 7*i, 2] + qnorm(0.975) * round(outtab_mom[2:4 + 7*i, 3], 3), 3),
                                      "]",
                                      sep = "")
  # assign 2sls estimates
  main_tab_mom[4, 1:3 + 3*i] <- round(outtab_mom[5:7 + 7*i, 2], 3)
  # assign 2sls standard errors
  main_tab_mom[5, 1:3 + 3*i] <- paste("(",round(outtab_mom[5:7 + 7*i, 3], 3), ")", sep = "")
  # assign 2sls confidence intervals
  main_tab_mom[6, 1:3 + 3*i] <- paste("[", 
                                      round(outtab_mom[5:7 + 7*i, 2] + qnorm(0.025) * round(outtab_mom[5:7 + 7*i, 3], 3), 3),
                                      "; ",
                                      round(outtab_mom[5:7 + 7*i, 2] + qnorm(0.975) * round(outtab_mom[5:7 + 7*i, 3], 3), 3),
                                      "]",
                                      sep = "")
  # assign baseline turnout rates
  main_tab_mom[7, 1:3 + 3*i] <- rep(round(outtab_mom[1 + 7*i, 2], 3), 3)
  # assign N
  main_tab_mom[8, 1:3 + 3*i] <- rep(sum(outtab_mom[1 + 7*i, 6:7]), 3)
  # assign share with twins
  main_tab_mom[9, 1:3 + 3*i] <- rep(round(outtab_mom[1 + 7*i, 7] / sum(outtab_mom[1 + 7*i, 6:7]), 3), 3)
  # assign F stat
  main_tab_mom[10, 1:3 + 3*i]<- round(f_mom[1:3 + 3*i, 3])
  # are covariates included?
  main_tab_mom[11,1:3 + 3*i] <- c("NO", "YES", "YES")
  main_tab_mom[12,1:3 + 3*i] <- c("NO", "NO", "YES")
}

# Set table up for fathers

main_tab_dad <- 
  matrix(NA, nrow = 12, ncol = 6)

colnames(main_tab_dad) <-
  rep(c("2013 Local", "2014 European"), each = 3)

rownames(main_tab_dad) <- 
  c("Twinning in",
    "first parity",
    "3",
    "No. of children (2sls)",
    "5",
    "6",
    "Turnout if one child in first parity",
    "N",
    "Share with twins",
    "F test",
    "2009 turnout",
    "2007 covariates")


for(i in 0:1){
  main_tab_dad[1, 1:3 + 3*i] <- round(outtab_dad[2:4 + 7*i, 2], 3)
  main_tab_dad[2, 1:3 + 3*i] <- paste("(",round(outtab_dad[2:4 + 7*i, 3], 3), ")", sep = "")
  main_tab_dad[3, 1:3 + 3*i] <- paste("[", 
                                      round(outtab_dad[2:4 + 7*i, 2] + qnorm(0.025) * round(outtab_dad[2:4 + 7*i, 3], 3), 3),
                                      "; ",
                                      round(outtab_dad[2:4 + 7*i, 2] + qnorm(0.975) * round(outtab_dad[2:4 + 7*i, 3], 3), 3),
                                      "]",
                                      sep = "")
  main_tab_dad[4, 1:3 + 3*i] <- round(outtab_dad[5:7 + 7*i, 2], 3)
  main_tab_dad[5, 1:3 + 3*i] <- paste("(",round(outtab_dad[5:7 + 7*i, 3], 3), ")", sep = "")
  main_tab_dad[6, 1:3 + 3*i] <- paste("[", 
                                      round(outtab_dad[5:7 + 7*i, 2] + qnorm(0.025) * round(outtab_dad[5:7 + 7*i, 3], 3), 3),
                                      "; ",
                                      round(outtab_dad[5:7 + 7*i, 2] + qnorm(0.975) * round(outtab_dad[5:7 + 7*i, 3], 3), 3),
                                      "]",
                                      sep = "")
  main_tab_dad[7, 1:3 + 3*i] <- rep(round(outtab_dad[1 + 7*i, 2], 3), 3)
  main_tab_dad[8, 1:3 + 3*i] <- rep(sum(outtab_dad[1 + 7*i, 6:7]), 3)
  main_tab_dad[9, 1:3 + 3*i] <- rep(round(outtab_dad[1 + 7*i, 7] / sum(outtab_dad[1 + 7*i, 6:7]), 3), 3)
  main_tab_dad[10, 1:3 + 3*i]<- round(f_dad[1:3 + 3*i, 3])
  main_tab_dad[11,1:3 + 3*i] <- c("NO", "YES", "YES")
  main_tab_dad[12,1:3 + 3*i] <- c("NO", "NO", "YES")
}

# print tables
xtable(main_tab_mom)
xtable(main_tab_dad)
